<template>
  <div class="login_container">
    <div class="login_box">
      <!-- Logo -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- Login form -->
      <el-form
        class="login_form"
        :rules="loginFormRules"
        ref="loginFormRef"
        :model="loginForm"
        status-icon
        @submit.native.prevent
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-s-custom"
            placeholder="请输入用户名"
            clearable
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            show-password
            clearable
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="button_div">
          <!--  -->
          <el-button
            type="primary"
            @click="onSubmit('loginFormRef')"
            native-type="submit"
            >登陆</el-button
          >
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    const patt = /^[a-zA-Z].*/;
    const validateUserName = (rule, value, callback) => {
      if (patt.test(value)) {
        callback();
      } else {
        callback(new Error("请以字母开头"));
      }
    };
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { validator: validateUserName, trigger: "blur" },
          {
            min: 5,
            message: "长度在 不小于 5 个字符",
            trigger: "blur",
          },
          {
            max: 15,
            message: "长度在 不大于 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            message: "长度在 不小于 6 个字符",
            trigger: "blur",
          },
          {
            max: 15,
            message: "长度在 不大于 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate(async (valided) => {
        if (!valided) {
          return;
        }

        try {
          // let response = await this.$http.post("Login", this.loginForm);
          let response = await this.$myhttp.post("Login", this.loginForm);
          if (response.status == 200) {
            this.$message.success("登陆成功");
            window.sessionStorage.setItem("token", response.data.token);
            this.$router.push("/home");
          } else {
            this.$message.error("登陆失败");
          }
        } catch (error) {
          console.log(error);
          this.$message.error("登陆失败");
          return;
        }
      });
    },
    resetForm() {
      this.$refs.loginFormRef.resetFields();
      for (const key in this.loginForm) {
        if (Object.hasOwnProperty.call(this.loginForm, key)) {
          this.loginForm[key] = "";
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  margin: 0;
  padding: 0;

  .login_box {
    background-color: white;
    border-radius: 5px;
    width: var(--loginbox-width);
    height: var(--loginbox-height);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      width: 120px;
      height: 120px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      border-radius: 50%;

      background-color: #fff;
      box-shadow: 0 0 10px #ddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: seashell;
      }
    }
    .login_form {
      width: 100%;
      position: absolute;
      bottom: 0;
      padding: 0 60px;
      box-sizing: border-box;
      .button_div {
        display: flex;
        justify-content: center;

        // transform: translate(50%);
      }
    }
  }
}
</style>
